package com.qf.test7;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadTest {
	public static final int ticket=10;
	public static int i=1;
	private static Lock lock =new ReentrantLock(true);
	public static void main(String[] args) {
		
		Thread t1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				Thread thread = Thread.currentThread();
				thread.setName("售票员1");
				
				while(i<ticket-1) {
					lock.lock();
					System.out.println(thread.getName()+"正在售出第"+i+"张票...");
					i++;
					lock.unlock();
				}
				
			}
		});
		Thread t2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				Thread thread = Thread.currentThread();
				thread.setName("售票员2");
				
				while(i<ticket-1) {
					lock.lock();
					System.out.println(thread.getName()+"正在售出第"+i+"张票...");
					i++;
					lock.unlock();
				}
				
			}
		});
		Thread t3 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				Thread thread = Thread.currentThread();
				thread.setName("售票员3");
				
				while(i<ticket-1) {
					lock.lock();
					System.out.println(thread.getName()+"正在售出第"+i+"张票...");
					i++;
					lock.unlock();
				}
				
			}
		});
		t1.start();t2.start();t3.start();
	}
}
